﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    class JLTAB50LSReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        public string ARTICLE ,COLORNO;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        Dictionary<string, int> qpplan = new Dictionary<string, int>();
        
        #endregion
        public JLTAB50LSReport()
            : base()
        {
        }
        public JLTAB50LSReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            else
            {
                Root = "C:\\IntexReport";
            }
                      						

        } 
        #region------------------------------Function------------------------
        /*CO	ABPAUFKRS	ABPAUFLFD	ABPAUFPOS	ABPART	ARTDESC	ABPFARBE	
                COLOR	ORDQTY	ALLOQTY	ABPNEUDAT	ABPNEUBEN	STATUS*/
        public void FillInExcelData(DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["AB50LS"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("ABPAUFKRS", typeof(System.String));
                dtOutput.Columns.Add("ABPAUFLFD", typeof(System.String));
                dtOutput.Columns.Add("ABPAUFPOS", typeof(System.String));
                dtOutput.Columns.Add("CO", typeof(System.String));
                dtOutput.Columns.Add("ABPART", typeof(System.String));
                dtOutput.Columns.Add("ARTDESC", typeof(System.String));
                dtOutput.Columns.Add("ORDQTY", typeof(System.String));
                dtOutput.Columns.Add("ALLOQTY", typeof(System.String));
                dtOutput.Columns.Add("ISSUEQTY", typeof(System.String));
                dtOutput.Columns.Add("STATUS", typeof(System.String));
                dtOutput.Columns.Add("ABPNEUDAT", typeof(System.String));
                dtOutput.Columns.Add("ABPNEUBEN", typeof(System.String));
                dtOutput.Columns.Add("ABPFARBE", typeof(System.String));
                dtOutput.Columns.Add("COLOR", typeof(System.String));
                dtOutput.Columns.Add("SPPART", typeof(System.String));
                dtOutput.Columns.Add("ZUEINTKG", typeof(System.String));
                dtOutput.Columns.Add("ZUENTNKG", typeof(System.String));
                dtOutput.Columns.Add("LSNAME2", typeof(System.String));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);

                foreach (DataRow rx in dtOutput.Rows)
                {//这里处理需要进行的数字和中文
                    rx["SPPART"] = Helper.GetGB2312Value(rx["SPPART"].ToString());
                    rx["LSNAME2"] = Helper.GetGB2312Value(rx["LSNAME2"].ToString());
                   
                }
                exAgent.DataTableToExcel(dtOutput, 2, 1);

                int count = dtOutput.Rows.Count;
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(2, 7, count + 1, 9);
                exAgent.SetCellValue(1, 1, "前缀");
                						
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }        
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                strCompany = "JLT";
                strExcelFile = @"JLT\JLTReport.xls";
                Standard = "AB50LS";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\JLT\JLTReport.xls");
                destfolder = SetFolder(Root, "AB50LS");
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                string newfilename = @"任务书领纱留位情况" + filedate;
                
                exAgent.CheckWorkBook(newfilename);
                ServerShareFile("", year, month, newfilename, fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ServerShareFile(string KRS, string year, string month, string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\" + fileabsname + ".xls"; ;
                destfile = filename;
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        private string CreateFile(string orderno)
        {
            string filename = "";
            try
            {
                string pwd = "";
                string sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, sheetName, pwd);
                filename = exAgent.FileName;
                exAgent.SetCurrentSheet(1);
                FillInExcelData(dsSource, 3, 1);
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
        #endregion
    }
}